package net.lsr.demo.controller.web;

import net.lsr.demo.domain.Qrcode;
import net.lsr.demo.helper.ContextHelper;
import net.lsr.demo.helper.FileHelper;
import net.lsr.demo.service.QrcodeService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * desc: 二维码控制器
 * @author lsr
 * @version 2014年6月4日
 */
@Controller
@RequestMapping("/qrcode")
public class QrcodeController {
	private static final Logger logger = LoggerFactory.getLogger(QrcodeController.class);
	@Autowired
	private QrcodeService qrcodeService;
	@Autowired
	private FileHelper fileHelper;
	@Autowired
	private ContextHelper contextHelper;
	
	@RequestMapping("/show")
	public String show(Model modal){
		logger.debug("show qrcode...");
		Qrcode q = qrcodeService.showDailyQrcode();
		if(q == null 
				|| StringUtils.isEmpty(q.getFilePath())
				|| !fileHelper.isFileExist(contextHelper.getContextPath() + q.getFilePath()))
			qrcodeService.generateQrcode();
		q = qrcodeService.showDailyQrcode();
		modal.addAttribute("qrcode", q);
		
		return "/qrcode/show";
	}
	
}
